<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Intents</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Android, programming, Intents, explicit intents,
implicit intents, tutorial, Java, mobile development">
<meta name="description" content="In this chapter of the Android tutorial, we will
talk about Intents.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content2">


<a href="/" title="Home">Home</a>&nbsp;
<a href=".." title="Home">Contents</a>


<h1>Intents</h1>


<p>
In this chapter of the Android development tutorial, we will talk about Intents.
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
According to the Android developer documentation, an <i>Intent</i> is an asynchronous message. 
It is an abstract description of an operation to be performed. Intents are used to navigate through 
activities. Activities, services and broadcast receivers are activated through intents. Intents 
enable loose coupling of code in the application. An Intent is passed to the some method like 
<code>Context.startActivity()</code> or <code>Context.startService()</code> to perform some action.
</p>

<p>
There are two types of intents. Explicit and implicit. In explicit intents you
provide the name of the Activity class. In implicit intents, you tell the
system what to do rather than name the Activity class to launch.
</p>


<h2>Implicit Intent</h2>

<p>
Displaying a web page can be done via an implicit intent. It will start
a default web browser with the specified web page. In the example we will display 
the contents of a web page. The manifest file is not modified.
</p>

<div class="codehead">main.xml</div>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" &gt;
 
    &lt;Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:onClick="onClicked"
        android:text="@string/btn_title" /&gt;
 
&lt;/LinearLayout&gt;
</pre>

<p>
In the <i>main.xml</i> layout file, we have just a simple button widget.
Clicking on the button will show the web page.
</p>

<div class="codehead">strings.xml</div>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
    &lt;string name="app_name"&gt;Url&lt;/string&gt;
    &lt;string name="btn_title"&gt;Visit&lt;/string&gt;
&lt;/resources&gt;
</pre>

<p>
String resources.
</p>

<div class="codehead">MainActivity.java</div>
<pre class="code">
package com.zetcode.url;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onClicked(View view)
    {
        Intent intent =  new Intent(Intent.ACTION_VIEW, 
            Uri.parse("http://www.google.com"));
        startActivity(intent);        
    }
}
</pre>

<p>
This is the MainActivity.java source file. 
</p>

<pre class="explanation">
public void onClicked(View view)
{
    Intent intent =  new Intent(Intent.ACTION_VIEW, 
        Uri.parse("http://www.google.com"));
    startActivity(intent);        
}
</pre>

<p>
In the <code>onClicked()</code> method, we create an Intent
object and start a new activity. With this implicit intent,
we tell Android to start a default web browser with google.com
web page opened.
</p>


<img src="/img/java/android/webpage.png" alt="Web page in Android emulator">
<div class="figure">
Figure: Web page in Android emulator
</div>


<h2>Explicit intent</h2>
 
<p>
In explicit intents, we provide the exact class to be run. 
</p>


<div class="codehead">AndroidManifest.xml</div>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.zetcode.explicit"
      android:versionCode="1"
      android:versionName="1.0"&gt;
    &lt;application android:label="@string/app_name" 
            android:icon="@drawable/ic_launcher"&gt;
        &lt;activity android:name="MainActivity"
                  android:label="@string/app_name"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
        
        &lt;activity android:name=".NextActivity"&gt;&lt;/activity&gt;
        
    &lt;/application&gt;
&lt;/manifest&gt;
</pre>

<p>
In the manifest file we register the new activity under the name NextActivity. 
The leading dot is a shorthand for the full package name, com.zetcode.explicit in our case.
</p>

<div class="codehead">main.xml</div>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    &gt;
    &lt;Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:onClick="onClicked"
        android:text="@string/btn_title" /&gt;        
&lt;/LinearLayout&gt;
</pre>

<p>
In the <i>main.xml</i> file we have one button. Clicking on this
button will start a new explicit activity.
</p>

<div class="codehead">strings.xml</div>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
    &lt;string name="app_name"&gt;Explicit&lt;/string&gt;
    &lt;string name="btn_title"&gt;Next&lt;/string&gt;
&lt;/resources&gt;
</pre>

<p>
This is the <i>strings.xml</i> resource file. 
</p>

<pre>
$ ls src/com/zetcode/explicit/
MainActivity.java  NextActivity.java
</pre>

<p>
In the src/com/zetcode/explicit subdirectory we have
two source files for two activities.
</p>

<div class="codehead">MainActivity.java</div>
<pre class="code">
package com.zetcode.explicit;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onClicked(View view)
    {
        Intent intent =  new Intent(this, NextActivity.class);
        startActivity(intent);        
    }
}
</pre>

<p>
This is the source for the main activity. In the onClicked() method, we 
start a new explicit intent. 
</p>

<pre class="explanation">
public void onClicked(View view)
{
    Intent intent =  new Intent(this, NextActivity.class);
    startActivity(intent);        
}
</pre>

<p>
The second parameter of the Intent constructor is the class name 
to be invoked. The activity is started with the <code>startActivity()</code> method.
</p>


<div class="codehead">NextActivity.java</div>
<pre class="code">
package com.zetcode.explicit;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.LinearLayout;

public class NextActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        initUI();
    }

    public void initUI()
    {
        LinearLayout lay = new LinearLayout(this);
        
        TextView tv = new TextView(this);
        tv.setText("Next screen");
        lay.addView(tv);

        setContentView(lay);
    }
}
</pre>

<p>
This is the <i>NextActivity.java</i> source code. In this activity, we show 
a TextView on the screen. It is programatically placed into a linear layout. 
</p>


<h2>Transfering data</h2>
 
<p>
Intents are used to transfer data from one activity to another.
We use the <code>putExtra()</code> method to add extra data
to an intent. In the following example, we write a name to the
edit text and click on the Send button. We will land on another 
screen where we will see a greeting to the name, that we have entered.
</p>

<pre>
$ ls res/layout/
screen1.xml  screen2.xml
$ ls src/com/zetcode/switch2/
FirstScreen.java  SecondScreen.java
</pre>

<p>
In the res/layout directory we have two XML layout files. In the
src/com/zetcode/switch2 we have source files of two activities.
</p>


<div class="codehead">AndroidManifest.xml</div>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.zetcode.switch2"
      android:versionCode="1"
      android:versionName="1.0"&gt;
    &lt;application android:label="@string/app_name"
                    android:icon="@drawable/ic_launcher"&gt;
        &lt;activity android:name=".FirstScreen"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
        
        &lt;activity android:name=".SecondScreen"&gt;&lt;/activity&gt;
        
    &lt;/application&gt;
&lt;/manifest&gt;
</pre>

<p>
In the manifest file we define two activities. The FirstScreen and the SecondScreen activity.
The FirstScreen is the main activity. 
</p>

<div class="codehead">screen1.xml</div>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    &gt;
  &lt;EditText android:id="@+id/editId"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dip"
          android:layout_marginBottom="10dip"
          android:hint="@string/etHint" /&gt;    
          
  &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/btn_send" 
          android:onClick="sendMessage" /&gt;      
&lt;/LinearLayout&gt;
</pre>

<p>
The <i>screen1.xml</i> layout file is loaded by the FirstScreen activity. 
It displays an EditText and a Button widget. The android:hint attribute 
shows a default shaded text in the EditText.
</p>

<div class="codehead">screen2.xml</div>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    &gt;
  &lt;TextView
      android:id="@+id/tvId"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      /&gt;
&lt;/LinearLayout&gt;
</pre>

<p>
In the <i>screen2.xml</i> file we have one TextView widget. 
It will display the text that we will transfer from one screen
to another. It is loaded by the SecondScreen activity.
</p>

<div class="codehead">strings.xml</div>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
    &lt;string name="app_name"&gt;Switch&lt;/string&gt;
    &lt;string name="etHint"&gt;Enter your name&lt;/string&gt;
    &lt;string name="btn_send"&gt;Send&lt;/string&gt;
&lt;/resources&gt;
</pre>

<p>
This is the <i>strings.xml</i> resource file. 
</p>


<div class="codehead">FirstScreen.java</div>
<pre class="code">
package com.zetcode.switch2;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class FirstScreen extends Activity
{
    private EditText iname;
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setTitle("First screen");
        setContentView(R.layout.screen1);

        iname = (EditText) findViewById(R.id.editId);
    }

    public void sendMessage(View view) 
    {
        Intent switchTo = new Intent(this, SecondScreen.class);        
        switchTo.putExtra("name", iname.getText().toString());        
        startActivity(switchTo); 
    }
}
</pre>

<p>
The FirstScreen is the main activity. The <code>sendMessage()</code> method
is called, when we click on the button. 
</p>

<pre class="explanation">
public void sendMessage(View view) 
{
    Intent switchTo = new Intent(this, SecondScreen.class);        
    switchTo.putExtra("name", iname.getText().toString());        
    startActivity(switchTo); 
}
</pre>

<p>
In the <code>sendMessage()</code> method, we create an instance of an Intent.
It will direct us to the SecondScreen activity. With the <code>putExtra()</code> method,
we add data from the EditText to the intent. The first parameter is the name by which
we will refer the data. The second parameter is the data to be transfered. 
</p>


<div class="codehead">SecondScreen.java</div>
<pre class="code">
package com.zetcode.switch2;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.TextView;

public class SecondScreen extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);

        setupUI();
    }

    void setupUI()
    {
        setTitle("Second screen");

        TextView tv = (TextView) findViewById(R.id.tvId);
        
        Intent i = getIntent();
        String name = i.getStringExtra("name");        
        tv.setText("You have entered: " + name);
    }
}
</pre>

<p>
This is the SecondScreen.java source file. It is called by the FirstScreen activity.
</p>

<pre class="explanation">
setupUI();
</pre>

<p>
In the setupUI() method, we set up the user interface of the screen.
</p>

<pre class="explanation">
setTitle("Second screen");
</pre>

<p>
We give the title to the screen with the <code>setTitle()</code> method.
</p>

<pre class="explanation">
Intent i = getIntent();
String name = i.getStringExtra("name");        
tv.setText("You have entered: " + name);
</pre>

<p>
The <code>getIntent()</code> method returns the intent that started the
activity. We get the extra data using the <code>getStringExtra()</code>
method. The data is set to the TextView.
</p>


<p>
In this chapter of the Android development tutorial, we have written about Intents.
</p>


<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div>
<br> 



<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified November 11, 2012  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

